//
//  WFCollectionViewLayout.m
//  谷居
//
//  Created by dllo on 16/6/20.
//  Copyright © 2016年 刘万方. All rights reserved.
//

#import "WFCollectionViewLayout.h"
#define WIDTH self.collectionView.frame.size.width
@interface WFCollectionViewLayout ()

@property (nonatomic, retain) NSMutableDictionary *columnDic; /** 用来存储每一列的Y值. */

@property (nonatomic, retain) NSMutableArray *attributesArray; /** 用来存储layout的属性的数组. */

@end
@implementation WFCollectionViewLayout

- (instancetype)init {
    self = [super init];
    if (self) {
        self.columnDic = [NSMutableDictionary dictionary];
        self.attributesArray = [NSMutableArray array];

    }
    return self;
}


/** 当collectionView布局layout的时候会调用这个方法. */
- (void)prepareLayout {
    [super prepareLayout];
    
    /** 根据列数进行遍历, 给骑士的每列都加上边界的距离. */
    
    /** 清空属性. */
    
    if (self.attributesArray != nil) {
        [self.attributesArray removeAllObjects];
    }

    
    /** 数据列数进行遍历. */
    for (NSInteger i = 0; i < self.columnCounts; i++) {
        NSString *key = [NSString stringWithFormat:@"%ld", i];
        self.columnDic[key] = @(self.edgeInsets.top);
    }
    
    /** 遍历每个item. */
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i < count; i++) {
        [self setItemFrame:i];
    }
}




- (void)setItemFrame:(NSInteger)index {
    __block NSString *minColumn = @"0";
    
    
    [self.columnDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if ([obj floatValue] < [self.columnDic[minColumn] floatValue]) {
            minColumn = key;
        }
        
    }];
    
    
    CGFloat width = ((WIDTH - self.edgeInsets.left - self.edgeInsets.right - (self.columnCounts - 1) * self.columnSpace)) / self.columnCounts;
    
    /** x坐标 = 左间距 + (列间距 + 宽) * 列的下标. */
    /** 列的小标对应的是最短下标. */
    CGFloat x = _edgeInsets.left + (width + _columnSpace) * [minColumn floatValue];
    
    /** 创建一个indexPath. */
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    
    
    /** 高. */
    CGFloat height = [self.delegate layout:self heightFroItenAtIndexPath:indexPath Width:width];
    
    
    /** 先找到当前最短一列的Y值. */
    CGFloat minY = [self.columnDic[minColumn] floatValue];
    
    /** 为最短的一列更新Y周的高度 = minY + 高 + 行间距 */
    self.columnDic[minColumn] = @(minY + height + self.rowSpace);
    
    
    
    /** 这个类用来设置 item 的 frame , bounds 等属性的. */
    UICollectionViewLayoutAttributes *layoutAttributed = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    layoutAttributed.frame = CGRectMake(x, minY, width, height);
    [self.attributesArray addObject:layoutAttributed];
    
    
    
}

#pragma mark - 第四个方法: 把所有的样式返回, 告诉系统如何布局item

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return self.attributesArray;
}




#pragma mark - 第五个方法设置滚动范围. 
- (CGSize)collectionViewContentSize {
    /** 滚动范围最长作为依据. */
    __block NSString *maxY = @"0";
    [self.columnDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        
        if ([obj floatValue] > [self.columnDic[maxY] floatValue]) {
            maxY = key;
        }
    }];

    
    CGFloat h = [self.columnDic[maxY] floatValue] + self.edgeInsets.bottom;
    return CGSizeMake(0, h);
}
@end
